package com.shengyuan.manage_os.old.ying.erpm.db;

import java.util.ArrayList;
import com.shengyuan.manage_os.old.ying.erpm.LoginActivity;

import android.content.Context;
import android.database.Cursor;

public class Actual_Data_Table extends DBHelper {

	private String Table_actual_form = "Table_actual_form";
	private String sql;
	public final static String Id="id",  JSON1 = "json1", JSON2="json2", 
			FINISHED ="finished", SENDED="sended", PRINTEABLED = "printenabled", FORM_TYPE="form_type",
			PCOUNT="printcount", OVERTIME= "overtime", SERVICEID="serviceid", USER="user";
	
	public Actual_Data_Table(Context ctx) {
		super(ctx);
		open();
		boolean condition = isTableExist(Table_actual_form);
		if (!condition) {
			sql = "CREATE TABLE "
					+ Table_actual_form
					+ "(id INTEGER PRIMARY KEY AUTOINCREMENT, form_type VARCHAR, json1 VARCHAR, json2 VARCHAR, "
					+ "user VARCHAR, finished INTEGER, overtime VARCHAR, serviceid INTEGER DEFAULT '0', "
					+ "sended INTEGER, printenabled INTEGER, printcount INTEGER)";
			execSQL(sql);
		}
	}
	
	/*本地缓存数据表
	 * 完成才可打印,并发送, 发送后 不能再显示
	 * form_type 表单名
	 * json1  第一次单 json2 第二次单
	 * user 操作人代码
	 * finished =0 末完成   =1 表单工作完成   
	 * sended = 0 没有发送 =1已经成功发送 
	 * printenabled 可打印 = 1 打印 = 0 不打印 
	 * printcount 打印次数
	 */
	public int Save_Data(String form_type, String json1, String json2, 
			String user, int finished, int sended, int printenabled, String overtime, int printcount) {
		if (printenabled ==0 ) {
			sended = 1;
		}
		String sql = 
				"INSERT INTO " + Table_actual_form
				+ "(form_type, json1, json2, user, finished, sended, printenabled, printcount, overtime) " 
				+ " VALUES(?,?,?,?,?,?,?,?,?)";
		mDb.execSQL(sql, new Object[] {form_type, json1, json2, user, 
				finished, sended, printenabled, printcount, overtime});
		
		sql = "select max("+ Id +") AS maxId from " + Table_actual_form;
		int maxid = GetValue(sql);
		return maxid ;
	}

	public void Update_Data(int rowid, String field, String value) {
		sql = "UPDATE " + Table_actual_form 
				+ " SET " + field + "='" + value +"'"
				+ " WHERE "+ Id +"=" + rowid;
		mDb.execSQL(sql);
	}
	
	
	/*
	 * finished=0 完成一半工作 
	 * finished=1 全部完成
	 */
	private Cursor List_Finished(int finished, String form_type) {
		if (form_type == null) {
			return findList(Table_actual_form, new String[]{JSON1, Id, PCOUNT, OVERTIME}, 
					"sended=0 and finished=? and user=? ", 
					new String[]{finished+"", LoginActivity.pmUser.ID +""},null, null, null  );

		}else {
			return findList(Table_actual_form, new String[]{JSON1, Id, PCOUNT, OVERTIME}, 
					"sended=0 and finished=? and user=? and form_type=?", 
					new String[]{finished+"", LoginActivity.pmUser.ID +"", form_type},null, null, null  );

		}
	}
	
	private Cursor List_AllFinished_CanPrint(int finished) {
			return findList(Table_actual_form, new String[]{JSON1, Id, PCOUNT, OVERTIME, JSON2}, 
					"printenabled=1 and sended=0 and finished=? and user=? ", 
					new String[]{finished+"", LoginActivity.pmUser.ID +""},null, null, null  );

	}
	
	private Cursor List_LastFinished_CanPrint(int finished) {
		return findList(Table_actual_form, new String[]{JSON1, Id, PCOUNT, OVERTIME, JSON2}, 
				"printenabled=1 and sended=0 and finished=? and user=? ", 
				new String[]{finished+"", LoginActivity.pmUser.ID +""},null, null, "ID DESC"  );

	}	
	
	public ArrayList<Integer> RowID;
	public ArrayList<Integer> PrintCount;
	public ArrayList<String> OverTime, Json2;
	public ArrayList<String> getArray_AllFinished_ForPrinter(int finished) {
		ArrayList<String> list = new ArrayList<String>();
		RowID = new ArrayList<Integer>();
		PrintCount = new ArrayList<Integer>();
		OverTime = new  ArrayList<String>();
		Json2 = new ArrayList<String>();
		Cursor cursor = List_AllFinished_CanPrint(finished);
		while (cursor.moveToNext()) {
			String s = cursor.getString(0);
			list.add(s);
			RowID.add(cursor.getInt(1));
			PrintCount.add(cursor.getInt(2));
			OverTime.add(cursor.getString(3));
			Json2.add(cursor.getString(4));
		}
		return list;
	}
	
	public ArrayList<String> getArray_LastFinished_ForPrinter(int finished) {
		ArrayList<String> list = new ArrayList<String>();
		RowID = new ArrayList<Integer>();
		PrintCount = new ArrayList<Integer>();
		OverTime = new  ArrayList<String>();
		Json2 = new ArrayList<String>();
		Cursor cursor = List_LastFinished_CanPrint(finished);
		while (cursor.moveToNext()) {
			String s = cursor.getString(0);
			list.add(s);
			RowID.add(cursor.getInt(1));
			PrintCount.add(cursor.getInt(2));
			OverTime.add(cursor.getString(3));
			Json2.add(cursor.getString(4));
			break;
		}
		return list;
	}
	
	
	public ArrayList<String> getArray_Finished(int finished, String form_type) {
		ArrayList<String> list = new ArrayList<String>();
		RowID = new ArrayList<Integer>();
		Cursor cursor = List_Finished(finished, form_type);
		while (cursor.moveToNext()) {
			String s = cursor.getString(0);
			list.add(s);
			RowID.add(cursor.getInt(1));
		}
		return list;
	}

	public long get_Sended_Last() {
		long i =0 ; 
		String yhdm = LoginActivity.pmUser.ID +"";
		if (!yhdm.equals("")) {
 			Cursor cursor =  findList(Table_actual_form, new String[]{
 					OVERTIME, Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
					"user=? AND form_type=?", 
					new String[]{ LoginActivity.pmUser.ID+"", "com.ying.erpm.Actual_OA_DaKa_JiDong"},null, null, " ID DESC"  );
			while (cursor.moveToNext()) {
				i = cursor.getInt(0);
				break;
			}	
		}
		return i;
	}
	
	public int get_Sended_DaKa() {
		int i =0 ; 
		Cursor cursor =  findList(Table_actual_form, new String[]{
				PCOUNT, Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
				"user=? AND form_type=?", 
				new String[]{ LoginActivity.pmUser.ID+"", "com.ying.erpm.Actual_OA_DaKa_JiDong"},null, null, " ID DESC"  );
		while (cursor.moveToNext()) {
			i = cursor.getInt(0);
			break;
		}	
		return i;
	}
	
	public int get_Sended_rows(String serviceid) {
		int i =0 ; 
		//String yhdm = LoginActivity.pmUser.ID +"";
		//if (!yhdm.equals("")) {
 			Cursor cursor =  findList(Table_actual_form, new String[]{
					Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
					"sended=1 and serviceid=?", 
					new String[]{ serviceid},null, null, null  );
			while (cursor.moveToNext()) {
				i++;
			}	
			
		//}
		return i;
	}

	public Cursor get_Sended_all_Cursor(String serviceid) {
		CLS_Actual_Data_Table cls = new CLS_Actual_Data_Table();
		Cursor  cursor =  findList(Table_actual_form, new String[]{
					Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
					"sended=1 and serviceid=?", 
					new String[]{  serviceid},null, null, null  );
//			while (cursor.moveToNext()) {
//				cls.Id = cursor.getInt(0);
//				cls.JSON1 = cursor.getString(1);
//				cls.JSON2 = cursor.getString(2);
//				cls.FINISHED = cursor.getInt(3);
//				cls.OVERTIME = cursor.getString(4);
//				cls.PCOUNT = cursor.getInt(5);
//				cls.SENDED = cursor.getInt(6);
//				cls.SERVICEID = cursor.getInt(7);
//				cls.USER = cursor.getString(8);
//				cls.FORM_TYPE = cursor.getString(9);
//				break;
//			}			
		return cursor;
	}
	
	public CLS_Actual_Data_Table get_Sended_all(String serviceid) {
		CLS_Actual_Data_Table cls = new CLS_Actual_Data_Table();
		String yhdm = LoginActivity.pmUser.ID +"";
		if (!yhdm.equals("")) {
 			Cursor cursor =  findList(Table_actual_form, new String[]{
					Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
					"sended=1 and serviceid=?", 
					new String[]{  serviceid},null, null, null  );
			while (cursor.moveToNext()) {
				cls.Id = cursor.getInt(0);
				cls.JSON1 = cursor.getString(1);
				cls.JSON2 = cursor.getString(2);
				cls.FINISHED = cursor.getInt(3);
				cls.OVERTIME = cursor.getString(4);
				cls.PCOUNT = cursor.getInt(5);
				cls.SENDED = cursor.getInt(6);
				cls.SERVICEID = cursor.getInt(7);
				cls.USER = cursor.getString(8);
				cls.FORM_TYPE = cursor.getString(9);
				break;
			}			
		}
		return cls;
	}
	
	public CLS_Actual_Data_Table get_Sended(String serviceid) {
		CLS_Actual_Data_Table cls = new CLS_Actual_Data_Table();
		String yhdm = LoginActivity.pmUser.ID +"";
		if (!yhdm.equals("")) {
 			Cursor cursor =  findList(Table_actual_form, new String[]{
					Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
					"sended=1 and serviceid=?", 
					new String[]{  serviceid},null, null, null  );
			while (cursor.moveToNext()) {
				cls.Id = cursor.getInt(0);
				cls.JSON1 = cursor.getString(1);
				cls.JSON2 = cursor.getString(2);
				cls.FINISHED = cursor.getInt(3);
				cls.OVERTIME = cursor.getString(4);
				cls.PCOUNT = cursor.getInt(5);
				cls.SENDED = cursor.getInt(6);
				cls.SERVICEID = cursor.getInt(7);
				cls.USER = cursor.getString(8);
				cls.FORM_TYPE = cursor.getString(9);
				break;
			}			
		}
		return cls;
	}
	
	public CLS_Actual_Data_Table get_Json1Data(int ID) {
		CLS_Actual_Data_Table cls = new CLS_Actual_Data_Table();
 			Cursor cursor =  findList(Table_actual_form, new String[]{
					Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
					"Id=?", 
					new String[]{  ID+"" },null, null, null  );
			while (cursor.moveToNext()) {
				cls.Id = cursor.getInt(0);
				cls.JSON1 = cursor.getString(1);
				cls.JSON2 = cursor.getString(2);
				cls.FINISHED = cursor.getInt(3);
				cls.OVERTIME = cursor.getString(4);
				cls.PCOUNT = cursor.getInt(5);
				cls.SENDED = cursor.getInt(6);
				cls.SERVICEID = cursor.getInt(7);
				cls.USER = cursor.getString(8);
				cls.FORM_TYPE = cursor.getString(9);
				break;
			}			
		return cls;
	}
	

	public CLS_Actual_Data_Table get_alllist(String serviceid) {
		CLS_Actual_Data_Table cls = new CLS_Actual_Data_Table();
		String yhdm = LoginActivity.pmUser.ID +"";
		if (!yhdm.equals("")) {
 			Cursor cursor =  findList(Table_actual_form, new String[]{
					Id, JSON1, JSON2,FINISHED, OVERTIME, PCOUNT, SENDED, SERVICEID, USER, FORM_TYPE}, 
					"user=? AND sended=1 ", 
					new String[]{ LoginActivity.pmUser.ID+""},null, null, null  );
			while (cursor.moveToNext()) {
				cls.Id = cursor.getInt(0);
				cls.JSON1 = cursor.getString(1);
				cls.JSON2 = cursor.getString(2);
				cls.FINISHED = cursor.getInt(3);
				cls.OVERTIME = cursor.getString(4);
				cls.PCOUNT = cursor.getInt(5);
				cls.SENDED = cursor.getInt(6);
				cls.SERVICEID = cursor.getInt(7);
				cls.USER = cursor.getString(8);
				cls.FORM_TYPE = cursor.getString(9);
			}			
		}
		return cls;
	}
	
}
